<?php


namespace app\index\controller;


use app\index\model\FacebookPage;
use app\index\model\MessageUser;

class MessageController extends BaseController
{
    public function getHomepageParam(){
        $home_page_list=FacebookPage::where('del_flag','=',0)->field('id,page_name')->select()->toArray();
        return json(['code'=>0,'home_page_list'=>$home_page_list]);
    }

    public function getHomePageList(){
        $page = input('post.pageIndex', 1);
        $size = input('post.pageSize', 10);
        $post = input('post.', [], 'trim');
        $search_param = [
            'page_id' => $post['page_id'] ?? null,
            'id' => $post['id'] ?? null,
        ];
        $page_param = [
            'page_num' => $page,
            'page_size' => $size,
        ];
        ['status' => $status, 'result' => $result] = FacebookPage::getHomePageList($search_param, $page_param);
        if ($status === false) {
            return json(['code' => 1, 'msg' => $result['msg']]);
        }
        return json(['code' => 0, 'product' => $result['product'], 'pageTotal' => $result['total']]);
    }

    public function addHomePage(){
        $data=input('post.');

        if(empty($data['page_id'])){
           return json(['code'=>1,'msg'=>'输入主页id']);
        }
        if(empty($data['page_name'])){
           return json(['code'=>1,'msg'=>'输入主页名称']);
        }
        if(empty($data['access_token'])){
           return json(['code'=>1,'msg'=>'输入access token']);
        }

        $FacebookPage=new FacebookPage();

        $check=$FacebookPage->where(function ($query)use($data){
            $query->where('page_id',$data['page_id'])->whereOr('page_name',$data['page_name'])->whereOr('access_token',$data['access_token']);
        })->where('del_flag',0)->field('id')->find();
        if(!empty($check)){
            return json(['code'=>1,'msg'=>'输入的数据已被使用']);
        }
        $home_page_data=['page_id'=>$data['page_id'],'page_name'=>$data['page_name'],'access_token'=>$data['access_token']];
        $result=$FacebookPage->insert($home_page_data);
        if($result===false){
            return json(['code'=>1,'msg'=>'添加失败']);
        }
        return json(['code'=>0,'msg'=>'添加成功']);
    }

    public function updateHomePage(){
        $data=input('post.');
        if(!isset($data['id']) || empty($data['id'])){
            return json(['code'=>1,'msg'=>'id不能为空']);
        }
        if(empty($data['page_id'])){
            return json(['code'=>1,'msg'=>'输入主页id']);
        }
        if(empty($data['page_name'])){
            return json(['code'=>1,'msg'=>'输入主页名称']);
        }
        $FacebookPage=new FacebookPage();
        $check=$FacebookPage->where('id','<>',$data['id'])->where(function ($query)use($data){
            $query->where('page_id',$data['page_id'])->whereOr('page_name',$data['page_name']);
                if(!empty($data['access_token'])){
                    $query->whereOr('access_token',$data['access_token']);
                }
        })->where('del_flag',0)->field('id')->find();
        if(!empty($check)){
            return json(['code'=>1,'msg'=>'输入的数据已被使用']);
        }

        $home_page_data=['page_id'=>$data['page_id'],'page_name'=>$data['page_name']];
        if(!empty($data['access_token'])){
            $home_page_data['access_token']=$data['access_token'];
        }
        $result=$FacebookPage->where('id','=',$data['id'])->update($home_page_data);
        if($result===false){
            return json(['code'=>1,'msg'=>'更新失败']);
        }
        return json(['code'=>0,'msg'=>'更新成功']);
    }

    public function sendMessage(){
        $page_id=input('post.page_id');
        $message='hello, world!';
        $page_data=FacebookPage::where('id','=',$page_id)->where('del_flag','=',0)->field('access_token')->find();

        $message_user_data=MessageUser::where('page_id','=',$page_id)->where('del_flag','=',0)->column('ps_id');

        ['status'=>$status,'msg'=>$msg]=FaceBook::sendMessage($page_data['access_token'],$message,$message_user_data);
        if($status===false){
            return json(['code'=>1,'msg'=>$msg]);
        }
        return json(['code'=>0,'msg'=>$msg]);
    }


}